Computer Science Engineering (CSE) Exam  >  Computer Science Engineering (CSE) Questions  >  What is the output for the given program?int ... Start Learning for Free
What is the output for the given program?
int sum(int arr[], int n)
{
int sum = 0; 
for (int i = 0; i < n; i++)
sum += arr[i];
return sum;
}
 main()
{
int arr[] = {12, 30, 40, 15};
int n = sizeof(arr) / sizeof(arr[0]);
cout << sum(arr, n);
return 0;
}
    Correct answer is '97'. Can you explain this answer?
    Most Upvoted Answer
    What is the output for the given program?int sum(int arr[], int n){int...
    The given program is incomplete and does not provide a clear output. It is missing the closing curly brace for the for loop and the main function.

    To get a valid output, we need to complete the program. Here's an example of a completed program that calculates the sum of an array:

    ```c++
    #include

    int sum(int arr[], int n) {
    int sum = 0;
    for (int i = 0; i < n;="" i++)="" />
    sum += arr[i];
    }
    return sum;
    }

    int main() {
    int arr[] = {1, 2, 3, 4, 5};
    int n = sizeof(arr) / sizeof(arr[0]);

    int result = sum(arr, n);
    std::cout < "sum="" of="" array="" elements:="" "="" />< result="" />< />

    return 0;
    }
    ```

    Output:
    ```
    Sum of array elements: 15
    ```
    Free Test
    Community Answer
    What is the output for the given program?int sum(int arr[], int n){int...
    The program is described as:
    In the given code, Consider the address of the array as 100.
    The main function has an array arrr[] = {12, 30, 40, 15};
    int n = sizeof(arr) / sizeof(arr[0]);
    sizeof(arr) = 16(4 x 2) i.e four elements and each size is 2. 
    sizeof(arr[0]) = Size of element arr[0] i.e first element size = 2.
    So n becomes n=16/2
    ∴ n=4
    And sum(arr, n)
    Here arr is the address of the array. 
    sum(100, 5)
    The sum function calculates the summation of elements
    Initially, the sum=0 sums each element and stores it in the sum variable.
    sum=12+30+40+15
    sum=97 and returns the sum variable to the main function i.e 97.
    Hence the correct answer is 97.
    Explore Courses for Computer Science Engineering (CSE) exam

    Top Courses for Computer Science Engineering (CSE)

    Question Description
    What is the output for the given program?int sum(int arr[], int n){int sum = 0;for (int i = 0; i < n; i++)sum += arr[i];return sum;}main(){int arr[] = {12, 30, 40, 15};int n = sizeof(arr) / sizeof(arr[0]);cout << sum(arr, n);return 0;}Correct answer is '97'. Can you explain this answer? for Computer Science Engineering (CSE) 2025 is part of Computer Science Engineering (CSE) preparation. The Question and answers have been prepared according to the Computer Science Engineering (CSE) exam syllabus. Information about What is the output for the given program?int sum(int arr[], int n){int sum = 0;for (int i = 0; i < n; i++)sum += arr[i];return sum;}main(){int arr[] = {12, 30, 40, 15};int n = sizeof(arr) / sizeof(arr[0]);cout << sum(arr, n);return 0;}Correct answer is '97'. Can you explain this answer? covers all topics & solutions for Computer Science Engineering (CSE) 2025 Exam. Find important definitions, questions, meanings, examples, exercises and tests below for What is the output for the given program?int sum(int arr[], int n){int sum = 0;for (int i = 0; i < n; i++)sum += arr[i];return sum;}main(){int arr[] = {12, 30, 40, 15};int n = sizeof(arr) / sizeof(arr[0]);cout << sum(arr, n);return 0;}Correct answer is '97'. Can you explain this answer?.
    Solutions for What is the output for the given program?int sum(int arr[], int n){int sum = 0;for (int i = 0; i < n; i++)sum += arr[i];return sum;}main(){int arr[] = {12, 30, 40, 15};int n = sizeof(arr) / sizeof(arr[0]);cout << sum(arr, n);return 0;}Correct answer is '97'. Can you explain this answer? in English & in Hindi are available as part of our courses for Computer Science Engineering (CSE). Download more important topics, notes, lectures and mock test series for Computer Science Engineering (CSE) Exam by signing up for free.
    Here you can find the meaning of What is the output for the given program?int sum(int arr[], int n){int sum = 0;for (int i = 0; i < n; i++)sum += arr[i];return sum;}main(){int arr[] = {12, 30, 40, 15};int n = sizeof(arr) / sizeof(arr[0]);cout << sum(arr, n);return 0;}Correct answer is '97'. Can you explain this answer? defined & explained in the simplest way possible. Besides giving the explanation of What is the output for the given program?int sum(int arr[], int n){int sum = 0;for (int i = 0; i < n; i++)sum += arr[i];return sum;}main(){int arr[] = {12, 30, 40, 15};int n = sizeof(arr) / sizeof(arr[0]);cout << sum(arr, n);return 0;}Correct answer is '97'. Can you explain this answer?, a detailed solution for What is the output for the given program?int sum(int arr[], int n){int sum = 0;for (int i = 0; i < n; i++)sum += arr[i];return sum;}main(){int arr[] = {12, 30, 40, 15};int n = sizeof(arr) / sizeof(arr[0]);cout << sum(arr, n);return 0;}Correct answer is '97'. Can you explain this answer? has been provided alongside types of What is the output for the given program?int sum(int arr[], int n){int sum = 0;for (int i = 0; i < n; i++)sum += arr[i];return sum;}main(){int arr[] = {12, 30, 40, 15};int n = sizeof(arr) / sizeof(arr[0]);cout << sum(arr, n);return 0;}Correct answer is '97'. Can you explain this answer? theory, EduRev gives you an ample number of questions to practice What is the output for the given program?int sum(int arr[], int n){int sum = 0;for (int i = 0; i < n; i++)sum += arr[i];return sum;}main(){int arr[] = {12, 30, 40, 15};int n = sizeof(arr) / sizeof(arr[0]);cout << sum(arr, n);return 0;}Correct answer is '97'. Can you explain this answer? tests, examples and also practice Computer Science Engineering (CSE) tests.
    Explore Courses for Computer Science Engineering (CSE) exam

    Top Courses for Computer Science Engineering (CSE)

    Explore Courses
    Signup for Free!
    Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
    10M+ students study on EduRev